<?php
/**
 * 文章系统分类 控制器
 * */
class ArticleclassAction extends Action
{
    /**
     * 管理已有的文章分类
     * */
    public function AdminIndex()
    {
        EasyACL('Articleclass','AdminIndex',$_SESSION);
        $Class = D('Articleclass');
        $count = $Class->count('','cid'); //计算总行数
        $listRows = 15; //每页显示数量
        $fields = 'cid,name,articleclass_sort';
        import('ORG.Util.Page');
        $p = new Page($count,$listRows);
        $list = $Class->findall(NULL,$fields,'articleclass_sort desc,cid desc',$p->firstRow . ',' .$p->listRows);
        $page = $p->show();
        //设置到模板
        $this->assign('Class',$list);
        $this->assign('page',$page);
        $this->display();
    }
    /**
     * 创建新的分类
     * */
    public function AdminCreate()
    {
        EasyACL('Articleclass','AdminCreate',$_SESSION);
        //POST
        if ($this->isPost()) {
            //
            $Input = Input::getInstance();
            $Input->filter('addslashes');

            $Class = D('Articleclass');
            $Class->name = $Input->post('name'); //分类名
            $Class->articleclass_sort = $Input->post('articleclass_sort'); //排序
            $time = time(); //获取运行时间
            $Class->created = $time;
            $Class->updated = $time;
            $result = $Class->add();
            //判断是否创建正确
            if(!$result) {
                //halt('尝试创建文章分类失败!');
                return $this->error($Class->getError());
            }
            $this->redirect('AdminIndex','Articleclass');
        }
        $this->display();
    }
    /**
     * 修改已有分类信息
     *
     * */
    public function AdminUpdate()
    {
        EasyACL('Articleclass','AdminUpdate',$_SESSION);
        $Input = Input::getInstance();
        $Input->filter('addslashes');
        //POST
        if($this->isPost()) {
            //设置查询条件
            import('ORG.Util.HashMap');
            $Conditions = new HashMap();
            $Conditions->put('cid',array('EQ',$Input->get('cid')));
            
            $Class = D('Articleclass');
            $Class->where($Conditions)->find();
            $Class->name = $Input->post('name');
            $Class->articleclass_sort = $Input->post('articleclass_sort');
            $Class->updated = time();
            $result = $Class->save();
            if(!$result) {
                halt('尝试修改分类失败!');
            }
            $this->redirect('AdminIndex','Articleclass');
        }

        import('ORG.Util.HashMap');
        $Conditions = new HashMap();
        $Conditions->put('cid',array('EQ',$Input->get('cid')));
        //获取要修改的分类
        $Class = D('Articleclass');
        $ClassContent = $Class->where($Conditions)->find();
        
        //设置到模板
        $this->assign('Class',$ClassContent);
        $this->display();
    }

    /**
     * 删除单个分类
     * */
    public function AdminDel($cid)
    {
        EasyACL('Articleclass','AdminDel',$_SESSION);
        $Class = D('Articleclass');
        $result = $Class->deleteById($cid);
        if(!$result){
            return FALSE;
        }
        return TRUE;
    }

    /**
     * 删除多个分类
     * */
    public function AdminDels()
    {
        EasyACL('Articleclass','AdminDels',$_SESSION);
        //POST
        if ($this->isPost()) {
            $cids = $_POST['cid'];
            $Error = '';
            foreach($cids as $id) {
                if(!$this->AdminDel($id)) {
                    $Error .= '尝试删除编号为' . $id . '分类失败<br />';
                }
            }
            if (empty($Error)) {
                $this->redirect('AdminIndex','Articleclass');
            } else {
                halt($Error);
            }
        }
        //GET
        if ($this->isGet()) {
            $Input = Input::getInstance();
            $Input->filter('addslashes');
            if (!$this->AdminDel($Input->get('cid'))) {
                halt('尝试删除编号为' . $Input->get('cid') . '分类失败');
            }
            $this->redirect('AdminIndex','Articleclass');
        }
    }
}
?>
